package com.blog.service;

import java.util.ArrayList;
import java.util.List;
import com.blog.dao.MenuDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blog.domain.Menu;


@Service("menuTree")
public class MenuTree {
	
	
	private List<Menu> treeList=new ArrayList<Menu>();
	private List<Menu> treeList_level1=new ArrayList<Menu>();
	private List<Menu> treeList_level2=new ArrayList<Menu>();
	private List<Menu> treeList_level3=new ArrayList<Menu>();
	private List<Menu> treeList_level4=new ArrayList<Menu>();
	
	@Autowired
	private MenuDao menuDAO;
	
	public void setAuthMenuTree(){
		treeList= menuDAO.getTreeQuery();
		//初始化四级列表
		for(Menu resource:treeList){
			if(resource.getResourceLevel()==1){
				treeList_level1.add(resource);
			}
			if(resource.getResourceLevel()==2){
				treeList_level2.add(resource);
			}
			if(resource.getResourceLevel()==3){
				treeList_level3.add(resource);
			}
			if(resource.getResourceLevel()==4){
				treeList_level4.add(resource);
			}
		}
	}
	/**
	 * @return 输出表示横向主菜单的HTML标记文本
	 * @see com.bips.core.framework.tree.BaseTree#getTreeView()
	 */
	public StringBuffer getMainTreeView() {
		StringBuffer results = new StringBuffer("");
		results.append("<div id=menu>\n");
		results.append("<ul class='menu' style='overflow:visible;border:0px;width:100%;'>\n");
		List<Menu> firstLevels=this.getChildrenRes(0L);
		results.append(this.getChildMenu(firstLevels));
		results.append("</ul>\n");
		results.append("</div>\n");
	return results;
  }
	/**
	 * 子菜单集合
	 * @param resources
	 * @return
	 */
	private StringBuffer getChildMenu(List<Menu> resources){
		StringBuffer results=new StringBuffer("");
		for(Menu resource:resources){
			results.append("<li>\n");
			List<Menu>children=this.getChildrenRes(resource.getId());
			if(children.size()>0){
				results.append("<a href='javascript:void(0)' class='parent' ><span>"+resource.getResourceName()+"</span></a>\n");
				results.append(this.appendChildMenu(children));
			}
			else{
//				results.append("<a href=\"www.baidu.com\">");
     			results.append("<a href=\"javascript:gotopage('"+resource.getResourceUrl()+"')\">");
				results.append("<span>"+resource.getResourceName()+"</span>");
				results.append(	"</a>\n");
			}
			results.append("</li>\n");
		}
		return results;
	}
	/**
	 * 子菜单集合
	 * @param resources
	 * @return
	 */
	private StringBuffer appendChildMenu(List<Menu> resources){
		StringBuffer results=new StringBuffer("");
		results.append("<div>\n");
		results.append("<ul>\n");
		results.append(this.getChildMenu(resources));
		results.append("</ul>\n");
		results.append("</div>\n");
		return results;
	}
	
	/**
	 * 获取子节点集合
	 * @param parentId
	 * @return
	 */
	private List<Menu>  getChildrenRes(Long parentId){
		List<Menu> children=new ArrayList<Menu>(); 
		for(Menu resource:treeList){
			if(resource.getParentId().equals(parentId)){
				children.add(resource);
			}
		}
		return children;
	}
}
